﻿Public Class SOFTWAREINGROUPPOLICY
    Dim connect As CONDB = CONDB.NewConnection()
    Dim publicPRGGroupid As String

    Public Sub New(prgGroupid As String, prgGroupName As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        publicPRGGroupid = prgGroupid
        softwareGroupIDLa.Text = prgGroupid
        softwareGroupNameLa.Text = prgGroupName
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub querySoftwarePolicie()
        Dim PRGGroupPolicy As New DataTable
        PRGGroupPolicy = connect.GetTable("SELECT a.`grppolid` AS 'GRPPOLID', a.`gprgid` AS 'รหัสกลุ่มซอฟต์แวร์', a.`prgid` AS 'รหัสซอฟต์แวร์', c.`prgcode` as 'Code ซอฟต์แวร์', c.`prgname` AS 'ชื่อซอฟต์แวร์', a.`padd` AS 'ADD', a.`pdel` AS 'DEL', a.`pupd` AS 'UPDATE', a.`pprt` AS 'PRINT', a.`psee` AS 'VIEW' FROM grppolicy a LEFT JOIN masgrpprg b ON a.gprgid = b.gprgid LEFT JOIN masprg c ON a.prgid = c.prgid WHERE a.gprgid = '" & publicPRGGroupid & "'")
        policyGrid.PrimaryGrid.DataSource = PRGGroupPolicy
    End Sub

    Private Sub filterSoftware()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(prgid AS CHAR(4)) AS PRGID, prgname, prgcode FROM masprg WHERE prgid NOT IN (SELECT prgid FROM grppolicy WHERE gprgid = '" & publicPRGGroupid & "');"
        s = New FILTERCLASS(prgListTxt, sql, "id software,ชื่อ software,โค้ด software", "100,140,100", "1,1,1", "1,1,1")
        s.SetShowInDown = True
        s.SetShowBorder = True
    End Sub

    Private Sub prgListTxt_Click(sender As Object, e As EventArgs) Handles prgListTxt.Click
        prgListTxt.Clear()
    End Sub

    Private Sub clearAddPolicieForm()
        prgListTxt.Clear()
        addChk.Checked = False
        delChk.Checked = False
        updateChk.Checked = False
        printChk.Checked = False
        viewChk.Checked = False
    End Sub

    Private Sub SOFTWAREINGROUPPOLICY_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        querySoftwarePolicie()
        filterSoftware()
    End Sub

    Private Sub policyGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles policyGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        If e.GridCell.ColumnIndex = 5 Or e.GridCell.ColumnIndex = 6 Or e.GridCell.ColumnIndex = 7 Or e.GridCell.ColumnIndex = 8 Or e.GridCell.ColumnIndex = 9 Then
            Dim editStat As Integer
            If e.GridCell.ColumnIndex = 5 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การเพิ่มข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 6 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การลบข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 7 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การแก้ไขข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 8 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การพิมพ์ข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 9 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การดูข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            End If

            If editStat = 1 Then
                connect = CONDB.NewConnection()

                Dim dtp As DataTable = connect.GetTable("SELECT padd, pdel, pupd, pprt, psee FROM grppolicy WHERE grppolid = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("GRPPOLID").Value() & "'")
                Dim padd As Boolean = dtp.Rows(0)("PADD")
                Dim pdel As Boolean = dtp.Rows(0)("PDEL")
                Dim pupd As Boolean = dtp.Rows(0)("PUPD")
                Dim pprt As Boolean = dtp.Rows(0)("PPRT")
                Dim psee As Boolean = dtp.Rows(0)("PSEE")

                Dim Sql As String
                Sql = ""

                If e.GridCell.ColumnIndex = 5 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ADD").Value() = True Then
                        Sql += "UPDATE grppolicy SET padd = '1'"
                    Else
                        If pdel = False And pupd = False And pprt = False And psee = False Then
                            Sql += "DELETE FROM grppolicy"
                        Else
                            Sql += "UPDATE grppolicy SET padd = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 6 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("DEL").Value() = True Then
                        Sql += "UPDATE grppolicy SET pdel = '1'"
                    Else
                        If padd = False And pupd = False And pprt = False And psee = False Then
                            Sql += "DELETE FROM grppolicy"
                        Else
                            Sql += "UPDATE grppolicy SET pdel = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 7 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("UPDATE").Value() = True Then
                        Sql += "UPDATE grppolicy SET pupd = '1'"
                    Else
                        If padd = False And pdel = False And pprt = False And psee = False Then
                            Sql += "DELETE FROM grppolicy"
                        Else
                            Sql += "UPDATE grppolicy SET pupd = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 8 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("PRINT").Value() = True Then
                        Sql += "UPDATE grppolicy SET pprt = '1'"
                    Else
                        If padd = False And pdel = False And pupd = False And psee = False Then
                            Sql += "DELETE FROM grppolicy"
                        Else
                            Sql += "UPDATE grppolicy SET pprt = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 9 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("VIEW").Value() = True Then
                        Sql += "UPDATE grppolicy SET psee = '1'"
                    Else
                        If padd = False And pdel = False And pupd = False And pprt = False Then
                            Sql += "DELETE FROM grppolicy"
                        Else
                            Sql += "UPDATE grppolicy SET psee = '0'"
                        End If
                    End If
                End If

                Sql += " WHERE grppolid = '"
                Sql += CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("grppolid").Value().ToString
                Sql += "'"
                'MsgBox(Sql)

                Try
                    connect.BeginTrans()
                    connect.ExecuteNonQuery(Sql)
                    connect.CommitTrans()
                    querySoftwarePolicie()
                Catch ex As Exception
                    connect.RollbackTrans()
                    MsgBox(ex.ToString)
                End Try
            Else
                querySoftwarePolicie()
            End If
        End If
    End Sub

    Private Sub grantPermissionBtn_Click(sender As Object, e As EventArgs) Handles grantPermissionBtn.Click
        If prgListTxt.Text.ToString.Length > 0 Then
            Dim objAddProgramAndPermission As New USERGROUPCLASS
            objAddProgramAndPermission.GPRGID_ = softwareGroupIDLa.Text
            objAddProgramAndPermission.PRGID_ = prgListTxt.Tag
            objAddProgramAndPermission.PADD_ = addChk.Checked
            objAddProgramAndPermission.PDEL_ = delChk.Checked
            objAddProgramAndPermission.PUPD_ = updateChk.Checked
            objAddProgramAndPermission.PPRT_ = printChk.Checked
            objAddProgramAndPermission.PSEE_ = viewChk.Checked

            If objAddProgramAndPermission.addSoftwareGroupPolicy() = True Then
                MsgBox("ท่านได้เพิ่มสิทธิ์การใช้โปรแกรมดังกล่าวให้แก่กลุ่มซอฟต์แวร์นี้แล้ว")
                querySoftwarePolicie()
                clearAddPolicieForm()
            End If

            connect.Dispose()
        Else
            MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        addChk.Checked = True
        delChk.Checked = True
        updateChk.Checked = True
        printChk.Checked = True
        viewChk.Checked = True
    End Sub
End Class